package file_operation;

import java.io.*;

public class BufferedReadWrite {
    /** 获取文件读取字符流缓冲区 */
    public static BufferedReader getReader(File file) throws FileNotFoundException {
        FileInputStream fis = new FileInputStream(file);
        return new BufferedReader(new InputStreamReader(fis));
    }

    /** 获取文件写入字符流缓冲区 */
    public static BufferedWriter getWriter(File file) throws FileNotFoundException {
        if (!isExist(file)) {
            System.out.println(file.getName() + "文件不存在");
            create(file);
        }
        FileOutputStream fos = new FileOutputStream(file);
        return new BufferedWriter(new OutputStreamWriter(fos));
    }

    /** 判断文件或目录是否存在 */
    public static boolean isExist(File file) {
        return file.exists();
    }

    /** 创建文件或目录 */
    public static void create(File file) {
        try {
            boolean b = file.createNewFile();
            if (b)
                System.out.println("创建文件：" + file.getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
